import 'package:flutter/material.dart';
import 'package:ioc_pay_app/l10n/Localization_intl.dart';

class LoginRoute extends StatefulWidget {
  @override
  _LoginRouteStatus createState() => _LoginRouteStatus();
}

class _LoginRouteStatus extends State<LoginRoute> {
  TextEditingController _usernameController = new TextEditingController();
  TextEditingController _passwordController = new TextEditingController();

  bool pwdShow = false;

  GlobalKey _formKey = new GlobalKey<FormState>();
  bool _nameAutoShow = true;

  @override
  void initState() {
    // 自动填充上次登录的用户名，填充后将焦点定位到密码输入框
    // _usernameController.text = Global.profile.lastLogin;
    // if (_usernameController.text != null) {
    //   _nameAutoShow = false;
    // }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var gm = GmLocalizations.of(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(gm.login),
      ),
      body: Padding(
        padding: EdgeInsets.all(16),
        child: Form(
            key: _formKey,
            child: Column(
              children: <Widget>[
                TextFormField(
                  autofocus: _nameAutoShow,
                  controller: _usernameController,
                  decoration: InputDecoration(
                    labelText: gm.userName,
                    hintText: gm.userName,
                    prefixIcon: Icon(Icons.person),
                  ),
                  validator: (v) {
                    return v.trim().isNotEmpty ? null : gm.userNameRequired;
                  },
                ),
                TextFormField(
                  controller: _passwordController,
                  autofocus: !_nameAutoShow,
                  obscureText: !pwdShow,
                  decoration: InputDecoration(
                      labelText: gm.password,
                      hintText: gm.password,
                      prefixIcon: Icon(Icons.lock),
                      suffixIcon: IconButton(
                        icon: Icon(
                            pwdShow ? Icons.visibility_off : Icons.visibility),
                        onPressed: () {
                          setState(() {
                            pwdShow = !pwdShow;
                          });
                        },
                      )),
                  validator: (v) {
                    return v.trim().isNotEmpty ? null : gm.passwordRequired;
                  },
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 25),
                  child: ConstrainedBox(
                    constraints: BoxConstraints.expand(height: 55.0),
                    child: RaisedButton(
                      color: Theme.of(context).primaryColor,
                      onPressed: _onLogin,
                      textColor: Colors.white,
                      child: Text(gm.login),
                    ),
                  ),
                )
              ],
            )),
      ),
    );
  }

  void _onLogin() async {}

// void _onLogin() async {
//   // 先验证各个表单字段是否合法
//   if ((_formKey.currentState as FormState).validate()) {
//     showLoading(context);
//     User user;
//     try {
//       user = await Git(context)
//           .login(_usernameController.text, _passwordController.text);
//       Provider.of<UserModel>(context, listen: false).user = user;
//     } catch (e) {
//       print(e);
//       if (e.response?.stateCode == 401) {
//         showToast(GmLocalizations.of(context).userNameOrPasswordWrong);
//       } else {
//         showToast(e.toString());
//       }
//     } finally {
//       Navigator.of(context).pop();
//     }
//     if (user != null) {
//       Navigator.of(context).pop();
//     }
//   }
// }
}
